﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;
using System.Collections.Generic;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class ListNews : System.Web.UI.Page
    {

        public Boolean IsVN = true;
        private Int32 danhmucID = 0;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private String Search = "";
        private String TenDanhMucTinTucVN = "";
        private String TenDanhMucTinTucOther = "";
        private DataTable dtProductCategorys = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucTinTucProvider.GetDanhMucTinTuc().Tables[0];

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                try
                {
                    danhmucID = Int32.Parse(Request.QueryString["DanhMucTinTucID"]);
                }
                catch
                {

                }
                
                try
                {
                    Search = Request.QueryString["txtSearch"].ToString();
                }
                catch
                {

                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);               
                RadTreeList1.ExpandToLevel(1);
                RadTreeList1.ExpandToLevel(2);

                RadTreeList2.ExpandToLevel(1);
                RadTreeList2.ExpandToLevel(2);

                CreateDDLCategory(0, 0);
                if (IsVN == true)
                {
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                    pnNhomOther.Visible = false;
                }
                else
                {
                    this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                    pnNhom.Visible = false;
                }
                ddlDanhmucSanPham.SelectedValue = danhmucID.ToString();
            }
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;

            for (Int32 i = 0; i < amountOfCategorySub; ++i)
            {
                Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucTinTucID"].ToString());
                ListItem li;
                if (languageCode == "vi-VN")
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucVN"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                else
                    li = new ListItem(preString + drCategorysSub[i]["TenDanhMucTinTucOther"].ToString(), drCategorysSub[i]["DanhMucTinTucID"].ToString());
                this.ddlDanhmucSanPham.Items.Add(li);
                CreateDDLCategory(subCategoryID, level + 1);
            }
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("ListNews.aspx?DanhMucTinTucID=" + ddlDanhmucSanPham.SelectedValue);
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ListNews.aspx?DanhMucTinTucID=" + danhmucID + "&&txtSearch=" + txtsearch.Text);
        }

        /// <summary>
        /// Ngon ngu tieng viet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
                RadTreeList1.DataBind();
            }
        }

        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                TenDanhMucTinTucVN = Search;
            else
                TenDanhMucTinTucOther = Search;
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
        }

        protected void RadTreeList1_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList1.CurrentPageIndex = e.NewPageIndex;
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList1.DataBind();
        }

        protected void RadTreeList1_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList1.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList1.DataBind();
        }

        /// <summary>
        /// Ngon ngu tieng anh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadTreeList2_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
                RadTreeList2.DataBind();
            }
        }

        protected void RadTreeList2_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                TenDanhMucTinTucVN = Search;
            else
                TenDanhMucTinTucOther = Search;
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
        }

        protected void RadTreeList2_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList2.CurrentPageIndex = e.NewPageIndex;
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList2.DataBind();
        }

        protected void RadTreeList2_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList2.DataSource = DanhMucTinTucProvider.GetDanhMucTinTucsFillter(danhmucID, TenDanhMucTinTucVN, TenDanhMucTinTucOther);
            RadTreeList2.DataBind();
        }

        protected void btnselect_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (languageCode == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                    arr.Add(str);
                }

            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucTinTucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                    arr.Add(str);
                }
            }
            if (arr.Count > 1 || arr.Count == 0)
            {
                if (IsVN == true)
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_Edit);
                else
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_EditOther);
                return;
            }
            else
            {
                Session["value"] = Int32.Parse(arr[0].ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "GetRadWindow().close()", true);
            }
        }
    }
}